/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jpThongKePNTheoQuy.java
 *
 * Created on Jan 10, 2012, 6:58:37 PM
 */
package GUI;

import Bussiness.PhieuXuat;
import DatabaseAccess.DBHelper01;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Admin
 */
public class jpThongKePXTheoQuy extends javax.swing.JPanel {

    static String bot;
    private DefaultTableModel modelTable = new DefaultTableModel();

    /** Creates new form jpThongKePNTheoQuy */
    public jpThongKePXTheoQuy() throws SQLException {
        bot = "";
        initComponents();
        Vector v = new Vector();
        PhieuXuat b = new PhieuXuat();
        v = b.getAllPhieuX();
        this.loadTable(v);
    }

    public void loadTable(Vector v) {
        modelTable.setNumRows(0);
        Vector v1 = new Vector();
        v1.addElement("Mã Hàng");
        v1.addElement("Số Phiếu");
        v1.addElement("Số Lượng Xuất");
        v1.addElement("Ngày Xuất");
        v1.addElement("Mã Khách Hàng");
        modelTable.setColumnIdentifiers(v1);
        PhieuXuat b = new PhieuXuat();
        for (int i = 0; i < v.size(); i++) {
            b = (PhieuXuat) v.get(i);
            Vector row = new Vector();

            row.addElement(b.getMaHang());
            row.addElement(b.getSoPhieu());
            row.addElement(b.getSlx());
            row.addElement(b.getNgayXuat());
            row.addElement(b.getMaKH());
            modelTable.addRow(row);
        }
        tblPhieuXuat.setModel(modelTable);
        int r = tblPhieuXuat.getSelectedRow();
        /*if(r < 0){
        this.bot = "";
        jButton2.setEnabled(false);
        jButton3.setEnabled(false);
        return;
        }
        else{
        bot = tblXuatHang.getValueAt(r, 0).toString();
        }*/
    }

    public void searchQuyI() {
        PhieuXuat b = new PhieuXuat();
        Vector v = new Vector();
        try {
            if (cboQuy.getSelectedIndex() == 0) {
                v = b.getPhieuXQuyI();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.loadTable(v);
    }

    public void searchQuyII() {
        PhieuXuat b = new PhieuXuat();
        Vector v = new Vector();
        try {
            if (cboQuy.getSelectedIndex() == 1) {
                v = b.getPhieuXQuyII();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.loadTable(v);
    }

    public void searchQuyIII() {
        PhieuXuat b = new PhieuXuat();
        Vector v = new Vector();
        try {
            if (cboQuy.getSelectedIndex() == 2) {
                v = b.getPhieuXQuyIII();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.loadTable(v);
    }

    public void searchQuyIV() {
        PhieuXuat b = new PhieuXuat();
        Vector v = new Vector();
        try {
            if (cboQuy.getSelectedIndex() == 3) {
                v = b.getPhieuXQuyIV();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.loadTable(v);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        cboQuy = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblPhieuXuat = new javax.swing.JTable();
        btnInBaoCao = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        btnThongKe = new javax.swing.JButton();

        jLabel4.setText("Qúy");

        cboQuy.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Quý I", "Quý II", "Quý III", "Quý IV" }));

        tblPhieuXuat.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblPhieuXuat);

        btnInBaoCao.setText("In Báo Cáo");
        btnInBaoCao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInBaoCaoActionPerformed(evt);
            }
        });

        jLabel1.setText("Thống Kê Hàng Hóa Đã Xuất Theo Quý ");

        btnThongKe.setText("Thống kê");
        btnThongKe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThongKeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(219, 219, 219)
                .addComponent(jLabel1)
                .addContainerGap(245, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(btnThongKe)
                .addGap(31, 31, 31)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addComponent(cboQuy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 270, Short.MAX_VALUE)
                .addComponent(btnInBaoCao)
                .addGap(60, 60, 60))
            .addGroup(layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 631, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel1)
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnInBaoCao)
                    .addComponent(btnThongKe)
                    .addComponent(jLabel4)
                    .addComponent(cboQuy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(41, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void btnThongKeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThongKeActionPerformed
// TODO add your handling code here:
    if (cboQuy.getSelectedIndex() == 0) {
        this.searchQuyI();
    }
    if (cboQuy.getSelectedIndex() == 1) {
        this.searchQuyII();
    }
    if (cboQuy.getSelectedIndex() == 2) {
        this.searchQuyIII();
    }
    if (cboQuy.getSelectedIndex() == 3) {
        this.searchQuyIV();
    }
}//GEN-LAST:event_btnThongKeActionPerformed

private void btnInBaoCaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInBaoCaoActionPerformed
// TODO add your handling code here:
    Connection con = new DBHelper01().DBHelper01();
    Map<String, Object> param = new HashMap<String, Object>();
    if (cboQuy.getSelectedIndex() == 0) {
        try {
            //"Documents\NetBeansProjects\DemoOnThi\src"
            String path = ".\\src\\ThongKeTheoQuyI.jrxml";
            JasperReport jasperReport = JasperCompileManager.compileReport(path);
            JasperPrint print = JasperFillManager.fillReport(jasperReport, param, con);
            //JasperPrint print = JasperFillManager.fillReport(path, param, con);
            JasperViewer.viewReport(print, false);
        } catch (Exception e) {
            try {
                String path = ".\\src\\ThongKeTheoQuyI.jrxml";
                JasperReport jasperReport = JasperCompileManager.compileReport(path);
                JasperPrint print = JasperFillManager.fillReport(jasperReport, param, con);
                //JasperPrint print = JasperFillManager.fillReport(path, param, con);
                JasperViewer.viewReport(print, false);
            } catch (JRException ex) {
                ex.printStackTrace();
            }
        }
    }
    if (cboQuy.getSelectedIndex() == 1) {
        try {
            //"Documents\NetBeansProjects\DemoOnThi\src"
            String path = ".\\src\\ThongKeTheoQuyII.jrxml";
            JasperReport jasperReport = JasperCompileManager.compileReport(path);
            JasperPrint print = JasperFillManager.fillReport(jasperReport, param, con);
            //JasperPrint print = JasperFillManager.fillReport(path, param, con);
            JasperViewer.viewReport(print, false);
        } catch (Exception e) {
            try {
                String path = ".\\src\\ThongKeTheoQuyII.jrxml";
                JasperReport jasperReport = JasperCompileManager.compileReport(path);
                JasperPrint print = JasperFillManager.fillReport(jasperReport, param, con);
                //JasperPrint print = JasperFillManager.fillReport(path, param, con);
                JasperViewer.viewReport(print, false);
            } catch (JRException ex) {
                ex.printStackTrace();
            }
        }
    }
    if (cboQuy.getSelectedIndex() == 2) {
        try {
            //"Documents\NetBeansProjects\DemoOnThi\src"
            String path = ".\\src\\ThongKeQuyIII.jrxml";
            JasperReport jasperReport = JasperCompileManager.compileReport(path);
            JasperPrint print = JasperFillManager.fillReport(jasperReport, param, con);
            //JasperPrint print = JasperFillManager.fillReport(path, param, con);
            JasperViewer.viewReport(print, false);
        } catch (Exception e) {
            try {
                String path = ".\\src\\ThongKeQuyIII.jrxml";
                JasperReport jasperReport = JasperCompileManager.compileReport(path);
                JasperPrint print = JasperFillManager.fillReport(jasperReport, param, con);
                //JasperPrint print = JasperFillManager.fillReport(path, param, con);
                JasperViewer.viewReport(print, false);
            } catch (JRException ex) {
                ex.printStackTrace();
            }
        }
    }
    if (cboQuy.getSelectedIndex() == 3) {
        try {
            //"Documents\NetBeansProjects\DemoOnThi\src"
            String path = ".\\src\\ThongKeHangXuatQuyIV.jrxml";
            JasperReport jasperReport = JasperCompileManager.compileReport(path);
            JasperPrint print = JasperFillManager.fillReport(jasperReport, param, con);
            //JasperPrint print = JasperFillManager.fillReport(path, param, con);
            JasperViewer.viewReport(print, false);
        } catch (Exception e) {
            try {
                String path = ".\\src\\ThongKeHangXuatQuyIV.jrxml";
                JasperReport jasperReport = JasperCompileManager.compileReport(path);
                JasperPrint print = JasperFillManager.fillReport(jasperReport, param, con);
                //JasperPrint print = JasperFillManager.fillReport(path, param, con);
                JasperViewer.viewReport(print, false);
            } catch (JRException ex) {
                ex.printStackTrace();
            }
        }
    }
}//GEN-LAST:event_btnInBaoCaoActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnInBaoCao;
    private javax.swing.JButton btnThongKe;
    private javax.swing.JComboBox cboQuy;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblPhieuXuat;
    // End of variables declaration//GEN-END:variables
}
